package org.easydev.base.webbase.bo;


import lombok.ToString;
import org.easydev.base.webbase.constants.ApiConst;

import java.io.Serial;
import java.io.Serializable;
import java.util.Optional;

@ToString
public class JsonEntity<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = -3505719368753191234L;

    private String code;

    private String message;

    private T data;

    public JsonEntity() {
    }

    public JsonEntity(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public JsonEntity(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }


    public static <T> JsonEntity<T> of(T data) {
        return new JsonEntity<>(ApiConst.RESPONSE_CODE_OK, ApiConst.RESPONSE_MESSAGE_OK, data);
    }

    public static <T> JsonEntity<T> ofNothing() {
        return  new JsonEntity<>(ApiConst.RESPONSE_CODE_OK, ApiConst.RESPONSE_MESSAGE_OK);
    }

    public static <T> JsonEntity<T> error(String errorMessage) {
        errorMessage = Optional.ofNullable(errorMessage).orElse(ApiConst.RESPONSE_MESSAGE_ERROR);
        JsonEntity<T> jsonEntity = new JsonEntity<>();
        jsonEntity.setCode(ApiConst.RESPONSE_CODE_ERROR);
        jsonEntity.setMessage(errorMessage);
        return jsonEntity;
    }

    public static <T> JsonEntity<T> customError(String errorCode, String errorMessage) {
        errorMessage = Optional.ofNullable(errorMessage).orElse(ApiConst.RESPONSE_MESSAGE_ERROR);
        JsonEntity<T> jsonEntity = new JsonEntity<>();
        jsonEntity.setCode(errorCode);
        jsonEntity.setMessage(errorMessage);
        return jsonEntity;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}
